package cn.javabb.admin.controller.pay;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.pay.GoodsQrDO;
import cn.javabb.core.service.pay.GoodsQrService;
import cn.javabb.log.action.DeleteAction;
import cn.javabb.log.action.StateAction;
import cn.javabb.log.annotation.Log;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 支付编码
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/09/16 10:34
 */
@Controller
@RequestMapping("/admin/goodsQr")
public class GoodsQrAdminController extends BaseController {
    @Autowired
    private GoodsQrService goodsQrService;

    @GetMapping("/list")
    public String listPage(){
        return "admin/pay/goodsQrList";
    }
    @GetMapping("/save")
    public String savePage(){
        return "admin/pay/goodsQrSave";
    }

    @ResponseBody
    @GetMapping("/")
    public Object list(GoodsQrDO goodsQr) {

        Page p =goodsQrService.getPage(getPage(),getSize(),goodsQr);
        return ResponseModel.table(p);
    }

    /**
     * 更新
     * @return
     */
    @ResponseBody
    @PostMapping("/")
    public Object saveOrUpdate(GoodsQrDO qr){
        int n = 0;
        if(null == qr.getId()){
            if(null == qr.getState()){qr.setState(0);}
            n = goodsQrService.insertSelective(qr);
        }else{
            n = goodsQrService.updateSelective(qr);
        }
        if(n>0){
            return ResponseModel.Success("保存成功");
        }
        return ResponseModel.Failure("保存失败");

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ResponseBody
    @DeleteMapping("/{ids}")
    @Log(name = "支付码删除",action = DeleteAction.class)
    public Object delete(@PathVariable String ids){
        if(StringUtils.isBlank(ids)){
            return ResponseModel.Failure("参数错误");
        }
        List<String> idList = ListUtils.stringToListString(ids);
        int num = goodsQrService.deleteByIds(GoodsQrDO.class, "id", idList);
        if(num>0){
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }

    /**
     * 更新状态
     * @param ids
     * @param state
     * @return
     */
    @ResponseBody
    @PostMapping("/updateState")
    @Log(name="支付码状态",action = StateAction.class)
    public Object updateState(String ids,Integer state){
        if(goodsQrService.updateState(ids,state)){
            return ResponseModel.Success("状态更新成功");
        }
        return ResponseModel.Failure("状态更新失败");
    }


}
